package com.kc.controller.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kc.bean.Article;
import com.kc.bean.Comment;
import com.kc.config.UploadFileConfig;
import com.kc.mapper.UserAccountMapper;
import com.kc.service.*;
import com.kc.service.impl.ArticleServiceImpl;
import com.kc.util.ResponesBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 此为测试专用接口，里面url为/test/**此路径一切都会放行
 */
@ControllerAdvice
@RestController
@RequestMapping("/test")
public class test {

    private Logger logger = LoggerFactory.getLogger(test.class);

    @Resource
    private UserAccountMapper userAccountMapper;

    @Resource
    private IRegistAndLogin iRegistAndLogin;

    @Resource
    private ResponesBean responesBean;

    @Resource
    private IUserAccountService userAccountService;

    @Cacheable(value = "userCache", key = "article")
    @PostMapping("/bzd")
    public Article bze(HttpServletRequest request,@RequestBody Article article) throws JsonProcessingException {
        String token = request.getHeader("AcessToken");
        String id = userAccountService.getAccount(token);
        responesBean.setAll(200,"test",article);
        return article;
    }

    /**
     * arr的value
     * "{"artcileId":"1","userid":"1","title":"1","publisher":"1","contene":"1","imgPath":"1","readingNumber":"1","commentsNumber":"1","classificationId":"1"}"
     * @param json
     * @return
     * @throws IOException
     */
    @PostMapping("/article")
    public ResponesBean userAccount(@RequestBody String json) throws IOException {
        logger.info("-------进入json测试方法---------");
        ObjectMapper mapper = new ObjectMapper();
        JsonNode arr = new ObjectMapper().readTree(json).get("article");
        System.out.println(arr);
        JsonNode arr1 = new ObjectMapper().readTree(json).get("password");
        String password = mapper.writeValueAsString(arr1);
        Article article = mapper.readValue(arr.toString(),Article.class);
        responesBean.setAll(200,"转换成功"+password,article);
        return responesBean;
    }


    @Resource
    private ArticleServiceImpl articleService;

    @Autowired
    private UploadFileConfig uploadFileConfig;

    @PostMapping("addarticle")
    public ResponesBean addarticle(@RequestBody String json,HttpServletRequest request) throws IOException {
//      开始处理数据
        ObjectMapper mapper = new ObjectMapper();
        JsonNode arr = new ObjectMapper().readTree(json).get("article");
        Article article = mapper.readValue(arr.toString(),Article.class);

        String lable = new ObjectMapper().readTree(json).get("lable").toString();
        lable = lable.substring(lable.indexOf("\"") + 1, lable.length() - 1);
        String[] lables = lable.split(",");

        String token = request.getHeader("AcessToken");

        responesBean = articleService.inster(article,lables,token);
        return responesBean;
    }


    @PostMapping("tijiao")
    public ResponesBean tijiao(@RequestParam MultipartFile file,String AimsId,String EditorType){
        responesBean = articleService.editor(file,AimsId,EditorType);
        return responesBean;
    }

    @Resource
    private IAccountLikeArticleService accountLikeArticleService;

    @PostMapping("addattention")
    public ResponesBean addattention(@RequestBody Article article,HttpServletRequest request){
        //获取用户token
        String token = request.getHeader("AcessToken");

        responesBean = accountLikeArticleService.LikeArticle(article,token);
        return responesBean;
    }

    @Resource
    private ICommentService commentService;

    @PostMapping("postacomment")
    public ResponesBean postacomment(@RequestBody Comment comment,HttpServletRequest request){

        String token = request.getHeader("AcessToken");
        responesBean = commentService.AddComment(comment,token);
        return responesBean;

    }

    @GetMapping("testcomm")
    public ResponesBean testcomm(){
        responesBean.setAll(200,"test",new Comment());
        return responesBean;
    }


    @PostMapping("testviewarticle")
    public ResponesBean viewarticle(String articleId){
        Map<String,Object> map = new HashMap<>();
        map = articleService.ViewArticle(articleId);
        responesBean.setAll(200,"success",map);
        return responesBean;
    }
}
